home *** CD-ROM | disk | FTP | other *** search
/ MacFormat UK 179 / MF_UK_179_1.iso / DiscContents / In the mag / Widgets / SurveyGizmo Widget 1.0 / SurveyGizmo Widget / SurveyGizmo.wdgt / scripts / surveyGizmo.js < prev   
Encoding:
JavaScript  |  2006-10-19  |  9.5 KB  |  203 lines

  1. // Get Surveys-Button could have a popup saying "get surveys for user-key xx" which also would have to be updated each time the api changes    - not implemented upto now...
  2.  
  3. // *****************
  4. // Config Settings
  5. // *****************
  6. var POSTURL = "https://api.surveygizmo.com/plugin.php?";
  7. var devKey = "hy6ba0hw52b8kdb18gl4ggs8";
  8. var myUserKey = "Please enter User-key";
  9. var GETURL = "https://www.surveygizmo.com/http_api/plugin.php?";
  10. var XMLCMD = "GETSURVEYLIST";
  11.  
  12. var ERR_ARR = new Array();
  13. // First 4 errorstrings are the strings returned by the surveygizmo-server so they should not be changed!!!
  14. ERR_ARR[0] = new Object(); ERR_ARR[0]["ErrString"] = "Invalid user ident".toUpperCase();        ERR_ARR[0]["ErrMsg"] = "Please check or add your User API Key"; ERR_ARR[0]["Spin"] = "";
  15. ERR_ARR[1] = new Object(); ERR_ARR[1]["ErrString"] = "Invalid developer key".toUpperCase();    ERR_ARR[1]["ErrMsg"] = "An invalid developer key was given"; ERR_ARR[1]["Spin"] = "";
  16. ERR_ARR[2] = new Object(); ERR_ARR[2]["ErrString"] = "Unrecognized Command".toUpperCase();    ERR_ARR[2]["ErrMsg"] = "An invalid command was given"; ERR_ARR[2]["Spin"] = "";
  17. ERR_ARR[3] = new Object(); ERR_ARR[3]["ErrString"] = "A command is required".toUpperCase();    ERR_ARR[3]["ErrMsg"] = "A command is required"; ERR_ARR[3]["Spin"] = "";
  18.  
  19. ERR_ARR[4] = new Object(); ERR_ARR[4]["ErrString"] = "Updating".toUpperCase();    ERR_ARR[4]["ErrMsg"] = "Updating Survey List..."; ERR_ARR[4]["Spin"] = '<img src="gfx/spinner.gif"/>'; 
  20. ERR_ARR[5] = new Object(); ERR_ARR[5]["ErrString"] = "InProgress".toUpperCase();    ERR_ARR[5]["ErrMsg"] = "Another request is in progress"; ERR_ARR[5]["Spin"] = "";
  21. ERR_ARR[6] = new Object(); ERR_ARR[6]["ErrString"] = "NoResponse".toUpperCase();    ERR_ARR[6]["ErrMsg"] = "No server response - contact SurveyGizmo"; ERR_ARR[6]["Spin"] = "";
  22. ERR_ARR[7] = new Object(); ERR_ARR[7]["ErrString"] = "WrongResponse".toUpperCase();    ERR_ARR[7]["ErrMsg"] = "Wrong server response"; ERR_ARR[7]["Spin"] = "";
  23. ERR_ARR[8] = new Object(); ERR_ARR[8]["ErrString"] = "ok".toUpperCase();    ERR_ARR[8]["ErrMsg"] = ""; ERR_ARR[8]["Spin"] = "";
  24.  
  25. var BASE_URL = "http://www.surveygizmo.com/";
  26. var AP_URL = "?ap=wdgty";
  27. var ROOT_URL = BASE_URL + AP_URL;
  28. var NEWSURVEY_URL = BASE_URL + "surveybuilder/create/" + AP_URL;
  29. var GETACCOUNT_URL = BASE_URL + "widget-new-account/" + AP_URL;
  30. var FINDKEY_URL = BASE_URL + "surveybuilder/account/#api";
  31. var UPDATE_URL = BASE_URL + "add-ons/survey-widget/"+ AP_URL;
  32.  
  33. var myVersion 
  34. var WIDGETVERSION = "1.0";                    // initial value only, will be read from the file above...
  35. var VERSIONFILE = "Info.plist";            // The file from which version will be extracted
  36. var VERSIONKEY = "CFBundleVersion";        // xml-key to look for
  37. var MAXREQUESTTIME = 20;    // No. of seconds to wait until the request is aborted
  38. var MYSTRINGLENGTH = 40;    // length of string to be shown in the popup-menu
  39. var btEditSurvey;
  40. var btReporting;
  41. var btPreview;
  42. var btDone;
  43. var btInfo;
  44. var btGetSurveys;
  45. var myTagValues = new Array();
  46. var requestOk = false;
  47. var conn = 0;
  48. var mySurveyNameBackup = "";
  49.  
  50. function readVersion(){
  51.     var myXmlDoc = new XMLHttpRequest();
  52.     var myInfoPList = "";
  53.     myXmlDoc.open("GET", VERSIONFILE, false);
  54.     myXmlDoc.send(null);
  55.     if (myXmlDoc.readyState == 4) {
  56.         var myInfoPList = myXmlDoc.responseText;
  57.         var myCFBundleVersionPos = myInfoPList.indexOf("CFBundleVersion");
  58.         var myVersionStart = myInfoPList.indexOf("</",myCFBundleVersionPos);
  59.         myVersionStart = myInfoPList.indexOf(">", myVersionStart); myVersionStart++;
  60.         myVersionStart = myInfoPList.indexOf(">", myVersionStart); myVersionStart++;
  61.         myVersionEnd = myInfoPList.indexOf("<", myVersionStart);
  62.         WIDGETVERSION = myInfoPList.substring(myVersionStart,myVersionEnd);
  63.     }
  64. }
  65.  
  66. function setup() {
  67.     setupButtons();
  68.     setPopups();
  69.     readPrefs();
  70.     readVersion();
  71.     document.getElementById("divWidgetVersion").innerText = WIDGETVERSION;
  72.     
  73.     
  74.     // Entry to "Create a new survey to get you started" -> shouldn't that be an entry to "create an account"?
  75.     document.getElementById("popupMenu").options[0] = new Option("Please select survey...",-2)
  76.     //document.getElementById("popupMenu").options[1] = new Option("Create a new survey",-1)
  77.     if (window.widget) {
  78.         widget.onshow = onshow;
  79.         //widget.onhide = onhide;
  80.     }
  81. }
  82.  
  83. function onshow(){ importxml(); }
  84. // function onhide(){ widget.onshow = ""; }
  85.  
  86. function setPopups(){
  87.     setPopupText(document.getElementById("btGetSurveys") , new Array("Get Your Surveys"));
  88.     setPopupText(document.getElementById("btEditSurvey") , new Array("Edit survey"));
  89.     setPopupText(document.getElementById("btReporting") , new Array("Reporting"));
  90.     setPopupText(document.getElementById("btPreview") , new Array("Preview surveys"));
  91.     
  92.     setJSLink(document.getElementById("blank"), ROOT_URL);
  93.     setPopupText(document.getElementById("blank") , new Array("Visit the SurveyGizmo website"));
  94.     
  95.     setJSLink(document.getElementById("lnkBtSurveyGizmoOnFront"), ROOT_URL);
  96.     setPopupText(document.getElementById("lnkBtSurveyGizmoOnFront") , new Array("Visit the SurveyGizmo website"));
  97.  
  98.     setJSLink(document.getElementById("lnkBtUpdateWidget"), UPDATE_URL);
  99.     setPopupText(document.getElementById("lnkBtUpdateWidget") , new Array("A new widget version is available at:" , UPDATE_URL));
  100.  
  101.     setJSLink(document.getElementById("lnkBtNewSurvey"), NEWSURVEY_URL);
  102.     setPopupText(document.getElementById("lnkBtNewSurvey") , new Array("Create a new survey to get started"));
  103.  
  104.     setJSLink(document.getElementById("lnkFindYourKey"), FINDKEY_URL);
  105.     setPopupText(document.getElementById("lnkFindYourKey") , new Array("Click to get your specific API key"));
  106.  
  107.     setJSLink(document.getElementById("lnkGetAccount"), GETACCOUNT_URL);
  108.     setPopupText(document.getElementById("lnkGetAccount") , new Array("Get your own SurveyGizmo account"));
  109.  
  110.     setJSLink(document.getElementById("lnkWdgty"), "http://www.wdgty.com/");
  111.     setPopupText(document.getElementById("lnkWdgty") , new Array("Visit the wdgty website"));
  112.  
  113.     setJSLink(document.getElementById("lnkSurveyGizmoOnBack"), ROOT_URL);
  114.     setPopupText(document.getElementById("lnkSurveyGizmoOnBack") , new Array("Visit the SurveyGizmo website"));
  115.  
  116. }
  117.  
  118.  
  119. function setJSLink(myObject, myLink){ myObject.href = "javascript:openURL('" + myLink + "');"; }
  120. function openURL(url) { (window.widget) ? widget.openURL(url) : window.location.href = url; }
  121.  
  122. /******************************
  123. * React on popup-menu changes *
  124. ******************************/
  125. function popupMenuChanged(elem) {
  126.     var chosenOption = elem.selectedIndex;
  127.     var myOption = elem.options[chosenOption].text;
  128.     var myValue = elem.options[chosenOption].value;
  129.     if (myValue > -1){
  130.         mySurveyNameBackup = document.getElementById("popupMenuText").innerText = myOption;    // change text of popupmenu (technically, the text that lies above the invisible popup-menu) and save Name
  131.         document.getElementById("outCompleted").innerText = myTagValues[myValue]["ctComplete"];
  132.         document.getElementById("outAbandoned").innerText = myTagValues[myValue]["ctAbandoned"];
  133.         document.getElementById("outInProgress").innerText = myTagValues[myValue]["ctInProgress"];
  134.         document.getElementById("outPartial").innerText = myTagValues[myValue]["ctPartial"];
  135.  
  136.         setJSLink(document.getElementById("lnkBtEditSurvey"), myTagValues[myValue]["link_edit"]);
  137.         setJSLink(document.getElementById("lnkBtReporting"), myTagValues[myValue]["link_reporting"]);
  138.         setJSLink(document.getElementById("lnkBtPreview"), myTagValues[myValue]["link_preview"]);
  139.  
  140.         document.getElementById("dvResults").style.display = "block";
  141.     } else if (myValue == -1){
  142.         document.getElementById("popupMenuText").innerText = myOption;
  143.     }
  144. }
  145.  
  146. function setPopupText(myObject, myLines){
  147.     myObject.title = "";
  148.     for (var i=0 ; i<myLines.length ; i++){
  149.         myObject.title = myObject.title + myLines[i] + "\n"; }
  150.     myObject.title = myObject.title.substring(0,myObject.title.length-1);
  151. }
  152.  
  153. function readPrefs(){
  154.     if (window.widget) {
  155.         var uKey = widget.preferenceForKey("userKey");
  156.         if (uKey != undefined){    // maybe we should test on length too?
  157.             document.getElementById("inpUserKey").value = myUserKey = uKey;
  158.         }
  159.     }
  160. }
  161.  
  162. function undoPrefs(){
  163.     document.getElementById("inpUserKey").value = myUserKey;
  164.     hidePrefs();
  165. }
  166.  
  167. function setPrefs(){
  168.     if (window.widget){
  169.         if (myUserKey != document.getElementById("inpUserKey").value){
  170.             alert("saved changed userkey");
  171.             myUserKey = document.getElementById("inpUserKey").value;
  172.             widget.setPreferenceForKey(myUserKey,"userKey");
  173.         }
  174.         hidePrefs();
  175.         importxml();
  176.     }
  177. }
  178.  
  179. // This function isn't needed as this is handled by hyperlinks
  180. function doNothing(){ }
  181.  
  182. function setupButtons(){
  183.     var myBtArray = new Object();
  184.     myBtArray["editsurvey"] = new Array("btEditSurvey","EDIT SURVEY");
  185.     myBtArray["reporting"] = new Array("btReporting","REPORTING");
  186.     myBtArray["preview"] = new Array("btPreview","PREVIEW");
  187.     myBtArray["done"] = new Array("btDone","Done");
  188.     myBtArray["cancel"] = new Array("btCancel","Cancel");
  189.     myBtArray["getsurveys"] = new Array("btGetSurveys","GET SURVEYS");
  190.     
  191.     btDone = new AppleButton( document.getElementById(myBtArray["done"][0]), myBtArray["done"][1],
  192.         23, "buttongfx/buttonLeft.png", "buttongfx/buttonleftclicked.png",
  193.         10, "buttongfx/buttonMiddle.png", "buttongfx/buttonmiddleclicked.png",
  194.         "buttongfx/buttonRight.png", "buttongfx/buttonrightclicked.png",
  195.         10, setPrefs);        
  196.     btCancel = new AppleButton( document.getElementById(myBtArray["cancel"][0]), myBtArray["cancel"][1],
  197.         23, "buttongfx/buttonLeft.png", "buttongfx/buttonleftclicked.png",
  198.         10, "buttongfx/buttonMiddle.png", "buttongfx/buttonmiddleclicked.png",
  199.         "buttongfx/buttonRight.png", "buttongfx/buttonrightclicked.png",
  200.         10, undoPrefs);
  201.         
  202.     btInfo = new AppleInfoButton(document.getElementById("btInfo"), document.getElementById("front"), "white", "white", showPrefs);
  203. }